<?php
/**品牌添加
 * Created by PhpStorm.
 * User: xiangpeng
 * Date: 2016/12/27
 * Time: 12:07
 */

namespace Admin\Controller;


use Common\Controller\AdminController;
use Common\Model\BrandModel;

class BrandController extends  AdminController
{
    //2.品牌的列表
    public function brandLists(){
        $brand=(new BrandModel())->select();
        //dd($brand);die;
        //分配数据
        $this->assign('brand',$brand);
        //加载模板
        $this->display();
    }

    //1.品牌添加方法  //3.编辑品牌
    public function add(){
        //3.1获得要编辑的id
        $bid=I('get.bid');
        //dd($bid);die;
        if(IS_POST){
            //获得post数据
            //dd($_POST);die;
            $data=I('post.');
            //3.2编辑
            if($bid){
                //echo 5;die;
                $data['bid']=$bid;
            }
                $info=$this->upload();
                $data['logo']=$info['data']['logo']['savepath'].$info['data']['logo']['savename'];
                //dd($data['logo']);die;
                $this->store(new BrandModel(),$data,function (){
                $this->success('操作成功',U('Brand/brandLists'));die;
            });

        }


        //echo 3;die;
        //获得当前编辑那一条的旧数据
        if($bid){
            $oldBrand=(new BrandModel())->where("bid={$bid}")->find();
            //dd($oldBrand);die;
            //分配数据
            $this->assign('oldBrand',$oldBrand);
        }

        //加载模板
        $this->display();

    }

    //品牌的删除
    public function del(){
        $info=$this->upload();
        //获得要删除的id
        $bid=I('get.bid');
        $logo =(new BrandModel())->where("bid={$bid}")->getField('logo');
        $logo='./Uploads/'.$logo;
        unlink($logo);
        if((new BrandModel())->where("bid={$bid}")->delete()){
            $this->success('删除成功',U('Brand/brandLists'));
        }else{
            $this->error('删除失败');
        }
        // 删除文件

        //dd($filename);die;
        //unlink($filename,$type='');
    }


    /**
     * 品牌LOGO图上传
     * @return [type] [description]
     */
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        //echo 2;DIE;
        if(!$info) {// 上传错误提示错误信息
            return ['status'=>'faild' ,'message'=>$upload->getError()];
        }else{// 上传成功
            return ['status'=>'success','message'=>'','data'=>$info];
        }
    }



}